class PoleMan extends egret.DisplayObjectContainer {
	private _bgContainer: egret.Sprite;//背景容器	
	private _container: egret.Sprite;//主容器

	private _touchOnOff: boolean = false;//屏幕是否点击 
	private _speed: number = 0.001;//加速度

	// private _hill: egret.Shape;//山实例
	private _hill: egret.Bitmap;

	private _bCloud: egret.Bitmap;//云实例
	private _sCloud: egret.Bitmap;//云实例

	private _dis: number = 0;//山体距离
	private _distance: number = 0;//山体距离
	private _hillWidth: number = 0;//山体宽度
	private _createHill: boolean = true;//可以创建山体

	private _pole: egret.Shape;//杆子实例
	// private _pole: egret.Bitmap;

	private _poleWidth: number = 0;//杆子长度
	private _createPole: boolean = true;//可以生成杆子
	private _rotationComplete: boolean = true;//旋转开关
	private _rotationOver: boolean = false;//失败旋转
	private _heroYAdd: boolean = false;//失败蜜蜂下滑

	private mcFactory: egret.MovieClipDataFactory;//动画工厂

	private _hero: egret.MovieClip;
	private _heroMove: boolean = false;//是否可以跑动
	private _heroRun: boolean = false;
	private _overOne: boolean = false;

	private _gameOverBgm: boolean = true;
	private _gameOver: boolean = false;
	private _switch: boolean = true;
	private _offsetWidth: number = 585;
	private _oneLoad: boolean = true;

	private _gameUI: GameUI;
	// private _sound: Sound;
	private _reawrdLevel: number = 1;
	public constructor() {
		super();

		this.touchEnabled = true;//可以点击
		// this.mcFactory = new egret.MovieClipDataFactory(RES.getRes("longzhu_json"), RES.getRes("longzhu_png"));
		this.mcFactory = new egret.MovieClipDataFactory(RES.getRes("mifeng_json"), RES.getRes("mifeng_png"));
		this.addContainer();//初始化 创建容器
	}
	/**
	 * 主循环
	 */
	private onLoop(e: egret.Event) {
		this.createHill();//随机生成山
		this.poleRice();//竹竿变长
		this.poleRotation();//竹竿旋转
		this.heroMove();//主角移动
		this.hillMove();//山移动
		this.gameOver();//游戏结束
		this.gameReplay();//重新开始
		// this.start();//开始游戏
		this.counts();//倒计时
		this.moveBCloud();//云移动动画
		this.moveSCloud();
		this.poleCut();//竹竿变短
	}
	/**
	 * 建立随机的山
	 */
	private createHill() {
		if (this._createHill) {
			this._dis = Math.random() * 450 + 60;
			this._distance = this._dis + this._container.x * -1;
			this._hillWidth = Math.random() * 100 + 50;
			// this._hill = GraphicsUtil.drawRect(this._distance, 800, this._hillWidth, 300);
			// this._container.addChild(this._hill);

			this._hill = new egret.Bitmap(RES.getRes('moun_png'))
			this._hill.x = this._distance;
			this._hill.y = 650;
			this._hill.width = this._hillWidth;
			this._hill.height = 300;
			this._container.addChild(this._hill);//添加第一座山
			this._container.setChildIndex(this._hill, 0);

			this._createHill = false;//建立山后关闭
		}
	}
	/**
	 * 竹竿变长
	 */
	private poleRice() {
		if (this._touchOnOff == true && this._createHill == false && this._createPole == false && this._pole && this._switch == true) {
			console.log("开始变长")
			var width = - + this._offsetWidth;
			this._pole.scaleY -= 8;
			if (this._pole.scaleY <= width) {
				this._switch = false
			}
		}
	}
	private poleCut() {
		if (this._touchOnOff == true && this._createHill == false && this._createPole == false && this._pole && this._switch == false) {
			console.log("开始变短")
			this._pole.scaleY += 8;
			if (this._pole.scaleY >= 0) {
				this._switch = true
			}
		}
	}
	/**
	 * 竹竿旋转
	 */
	private poleRotation() {
		if (this._touchOnOff == false && this._createHill == false && this._createPole == false && this._pole && this._rotationComplete == true) {
			this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touch_begin, this);
			this._poleWidth = this._pole.scaleY * -1 + 40;//获取竹竿长度 + 版边的宽度
			this._pole.rotation += 3 + this._speed;
			this._speed++;
			if (this._pole.rotation >= 90) {
				this._pole.rotation = 90;
				this._speed = 0.001;
				this._rotationComplete = false;
				this._heroMove = true;
			}
		}
	}
	private heroMove() {
		if (this._heroMove == true) {
			if (this._heroRun == false) {
				this._hero.gotoAndPlay("run", -1);
				this._heroRun = true;
			}

			///////////////////////////////////////////////////////////////////////////////////	
			if (this._poleWidth >= this._dis - 8 && this._poleWidth <= this._dis + this._hillWidth) {
				console.log("长度正好");
				this._hero.x += 5;
				if (this._hero.x >= this._distance + this._hillWidth - 15 - this._hero.width) {
					this._hero.x = this._distance + this._hillWidth - 15 - this._hero.width + 15
					this._hero.gotoAndPlay("stand", -1);
					this._heroMove = false;
					this._gameUI.Score += 1;//步数加1
					this._overOne = true;
					this._container.removeChild(this._pole);
				}

			}
			if (this._poleWidth < this._dis - 8) {
				console.log("竹竿短了");
				this._hero.x += 5;
				if (this._hero.x >= this._container.x * -1 + this._poleWidth + 5) {
					this._hero.x = this._container.x * -1 + this._poleWidth + 5;
					// this._hero.x = this._container.x * -1 + this._poleWidth;
					this._hero.gotoAndPlay("stand", -1);
					if (this._hero.x == this._container.x * -1 + this._poleWidth + 5 && this._rotationOver == false) {
						this._pole.anchorOffsetX = 5;
						this._pole.rotation += 10;
						this._hero.rotation = 60;
						if (this._pole.rotation >= 180) {
							// this._hero.y = 850;
							this._pole.rotation = 180;
							this._rotationOver = true;
							// // this._container.removeChild(this._hero);
							// console.log("游戏结束");
							// this._heroMove = false;
							// this._gameOver = true;//游戏结束	
						}
					}
					if (this._hero.x == this._container.x * -1 + this._poleWidth + 5 && this._heroYAdd == false) {
						this._gameOver = true;//游戏结束
						this._hero.y += 20;
						// this._gameUI.playGameOver()
						// if (this._hero.y >= 850) {
						if (this._hero.y >= 800) {
							this._container.removeChild(this._hero)
							// this._hero.y = 850;
							this._heroYAdd = true;
							this._heroMove = false;

						}
					}
					if (this._gameOverBgm) {
						this._gameUI.playGameOver()
						this._gameOverBgm = false
					}
				}

			}
			if (this._poleWidth > this._dis + this._hillWidth) {
				console.log("竹竿太长了");
				this._hero.x += 5;
				if (this._hero.x >= this._container.x * -1 + this._poleWidth + 5) {
					this._hero.x = this._container.x * -1 + this._poleWidth + 5;
					// this._hero.x = this._container.x * -1 + this._poleWidth;
					this._hero.gotoAndPlay("stand", -1);
					if (this._hero.x == this._container.x * -1 + this._poleWidth + 5 && this._rotationOver == false) {
						this._pole.anchorOffsetX = 5;
						this._pole.rotation += 10;
						this._hero.rotation = 60;
						if (this._pole.rotation >= 180) {
							// this._hero.y = 800;
							this._pole.rotation = 180;
							this._rotationOver = true;
							// this._container.removeChild(this._hero);
							// console.log("游戏结束");
							// this._heroMove = false;
							// this._gameOver = true;//游戏结束
						}

					}
					if (this._hero.x == this._container.x * -1 + this._poleWidth + 5 && this._heroYAdd == false) {
						this._gameOver = true;//游戏结束
						this._hero.y += 20;
						// this._gameUI.playGameOver()
						// if (this._hero.y >= 850) {
						if (this._hero.y >= 800) {
							// this._hero.y = 850;
							this._container.removeChild(this._hero)
							this._heroYAdd = true;
							this._heroMove = false;

						}
					}
					if (this._gameOverBgm) {
						this._gameUI.playGameOver()
						this._gameOverBgm = false
					}
				}

			}
			///////////////////////////////////////////////////////////////////////////////////				

		}
	}
	/**
	 * 山移动
	 */
	private hillMove() {
		if (this._overOne == true) {
			var that = this;
			this._container.x -= 8;
			var aTob: number = (this._distance + this._hillWidth) * -1 + 50;
			if (this._container.x <= aTob) {
				this._container.x = aTob;

				this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touch_begin, this);
				this._createHill = true;
				this._createPole = true;
				this._rotationComplete = true;
				this._heroMove = false;
				this._heroRun = false;
				this._overOne = false;

				if (this._gameUI.Score == this._reawrdLevel) {
					this._gameUI.reward()
					this._gameUI._count = false;
					this._gameUI._start = false;
					this._gameUI.clear();
				} else {
					this._gameUI.Count = this.urlParam('countdown') - 0 || 10;
					setTimeout(function () {
						that._gameUI.timer();
						that._gameUI._start = true;
					}, 200);
				}
			}
		}
	}
	/**
	 * 游戏结束
	 */
	private gameOver() {
		if (this._gameOver == true) {
			console.log('游戏结束')
			// this._container.removeChildren();
			this._gameOver = false;
			this._gameUI._start = false;
			// this._gameUI.showOver();
			// this._gameUI.playGameOver();
		}
	}
	/**
	 * 重新游戏
	 */
	private gameReplay() {
		if (this._gameUI._rePlay == true) {
			console.log("重新游戏");
			///////////初始化//////////
			this._container.x = 0;//容器初始化
			this._pole = null;
			this._createHill = true;//重新建立山
			this._createPole = true;//重新建立竹竿
			this._rotationComplete = true;//重新开启旋转
			this._heroMove = false;//重新定义角色
			this._heroRun = false;//重新定义角色状态
			this._rotationOver = false;//失败旋转
			this._heroYAdd = false;

			this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touch_begin, this);

			// this._hill = GraphicsUtil.drawRect(-50, 800, 100, 300);
			this._hill = new egret.Bitmap(RES.getRes('moun_png'))
			this._hill.x = -50;
			this._hill.y = 650;
			this._hill.width = 100;
			this._hill.height = 300;

			this._container.addChild(this._hill);//重新添加第一座山
			this._container.setChildIndex(this._hill, 0);

			// this._hero = new egret.MovieClip(this.mcFactory.generateMovieClipData("longzhu"));
			this._hero = new egret.MovieClip(this.mcFactory.generateMovieClipData("mifeng"))
			this._hero.gotoAndPlay("stand", -1);
			this._hero.x = 0;
			this._hero.y = 650 - this._hero.height;
			this._container.addChild(this._hero);//添加主角
			this._container.setChildIndex(this._hero, 2);

			/////////////////////////
			this._gameUI._rePlay = false;
			this._gameUI._count = false;
			this._gameUI.Count = this.urlParam('countdown') - 0 || 10;
			this._gameUI.clear();
			this._gameUI.timer();
			this._gameUI._start = true;
		}
	}
	/**
	 * 点击 抬起事件
	 */
	private touch_begin(e: egret.TouchEvent) {
		if (this._oneLoad == true) {
			this._oneLoad = false;
			this._gameUI.onClick_start();
			this._touchOnOff = true;
			this.addPole()
		} else if (this._gameUI._start) {
			console.log("鼠标按下了");
			this._touchOnOff = true;
			this.addPole();//建立竹竿
		}
	}
	private touch_end(e: egret.TouchEvent) {
		if (this._touchOnOff) {
			console.log("鼠标抬起了");
			this._touchOnOff = false;

			this._gameUI._count = false;
			this._gameUI.clear();
			this._gameUI._start = false;
		}
	}

	/**
	 * 建立竹竿
	 */
	private addPole() {
		if (this._createHill == false && this._createPole == true && this._gameUI._start == true) {
			var aTob: number = this._container.x * -1 + 45;
			if (!this._pole) {
				console.log("建立一根竹竿");
				this._pole = GraphicsUtil.drawRect(45, 655, 10, 1, 0xA2E04A);
				this._container.addChild(this._pole);

				this._container.setChildIndex(this._pole, 2);
			}
			else {
				console.log("建立第二根竹竿");
				// this._container.removeChild(this._pole);
				this._pole = GraphicsUtil.drawRect(aTob, 655, 10, 1, 0xA2E04A);
				this._container.addChild(this._pole);

				this._container.setChildIndex(this._pole, 2);
			}
			this._createPole = false;//建立竹竿后关闭
		}
	}

	private start() {
		if (this._gameUI._start == true) {
			this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touch_begin, this);
			this.addEventListener(egret.TouchEvent.TOUCH_END, this.touch_end, this);
		}
	}

	private counts() {
		if (this._gameUI._count == true) {
			if (this._gameUI.Count == 0) {
				if (this._gameOverBgm) {
					this._gameUI.playGameOver()
					this._gameOverBgm = false
				}
				this._gameOver = true;
				this._gameUI._count = false;
				this._gameUI.clear();
			}
		}
	}

	private moveBCloud() {
		if (this._bCloud.x < -163) {
			this._bCloud.x = 640;
			return;
		} else {
			this._bCloud.x -= 0.4;
		}
	}
	private moveSCloud() {
		if (this._sCloud.x < -97) {
			this._sCloud.x = 640;
			return;
		} else {
			this._sCloud.x -= 0.2;
		}
	}

	/**
	 * 初始化
	 */
	private addContainer() {

		this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touch_begin, this);
		this.addEventListener(egret.TouchEvent.TOUCH_END, this.touch_end, this);
		this.addEventListener(egret.Event.ENTER_FRAME, this.onLoop, this);

		this._bgContainer = new egret.Sprite();
		this.addChild(this._bgContainer);//添加背景容器

		this._container = new egret.Sprite();
		this.addChild(this._container);//添加主容器

		// 背景画板
		var bg: egret.Shape = new egret.Shape();
		bg = GraphicsUtil.drawRect(0, 0, 640, 960, 0xffffff, 0);
		this._bgContainer.addChild(bg);
		// 云
		this._bCloud = new egret.Bitmap(RES.getRes('b_cloud_png'));
		this._bCloud.x = 184.5;
		this._bCloud.y = 89;
		this._bgContainer.addChild(this._bCloud);

		this._sCloud = new egret.Bitmap(RES.getRes('s_cloud_png'));
		this._sCloud.x = 466;
		this._sCloud.y = 0;
		this._bgContainer.addChild(this._sCloud);

		// 获取屏幕宽度尺寸
		this._offsetWidth = this.width - 55;

		// 添加第一座山
		this._hill = new egret.Bitmap(RES.getRes('moun_png'))
		this._hill.x = -50;
		this._hill.y = 650;
		this._hill.width = 100;
		this._hill.height = 300;
		this._container.addChild(this._hill);//添加第一座山
		this._container.setChildIndex(this._hill, 0);

		// 主角
		this._hero = new egret.MovieClip(this.mcFactory.generateMovieClipData("mifeng"));
		this._hero.gotoAndPlay("stand", -1);
		this._hero.x = 0;
		this._hero.y = 650 - this._hero.height;
		this._container.addChild(this._hero);//添加主角

		this._container.setChildIndex(this._hero, 2);

		this.addChild(this._gameUI = new GameUI());

		// 游戏初始数据
		this._reawrdLevel = this.urlParam('awardLevel') - 0 || 0;	//	领奖关数
		// this._gameUI.Count = this.urlParam('countdown') - 0 || 10;	//	倒计时
		// this._gameUI.countdown = this.urlParam('countdown') - 0 || 10;	//	倒计时
		// this._gameUI.Score = this.urlParam('startLevel') - 0 || 0;	//	开始关数
		// this._gameUI.userId = this.urlParam('userID') - 0 || 0;	//	userId

	}
	// 获取url参数
	private urlParam(type) {
		var _s = window.location.search,
			attr = {};

		if (_s.indexOf("?") >= 0) {
			_s = _s.substring(1);
			var arry = _s.split('&');
			for (var i = 0; i < arry.length; i++) {
				var item = arry[i];
				attr[item.split("=")[0]] = item.split("=")[1];
			}
		}
		return attr[type];
	}
}